package View;

import Controller.Controller;
import Model.Model;

import javax.swing.*;
import java.awt.*;
public class View extends JFrame
{
    public JPanel jPanel0;
    public JPanel jPanel1;
    public JPanel jPanel2;

    public View()
    {
        setTitle("Java 第一个 GUI 程序");    //设置显示窗口标题
        setSize(600,300);    //设置窗口显示尺寸100
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //置窗口是否可以关闭
        JLabel jlabel0=new JLabel("这是使用JFrame类创建的窗口");    //创建一个标签

        jPanel0=new JPanel();    //创建文本框面板
        JTextField txtfield0=new JTextField(20);    //创建文本框
        txtfield0.setText("普通文本框");    //设置文本框的内容
        txtfield0.addActionListener(new Controller());
        JButton textButton = new JButton("文本配套按钮");
        textButton.addActionListener(new Controller());
        jPanel0.add(textButton);
        jPanel0.add(txtfield0);
        jPanel0.add(jlabel0);

        jPanel1=new JPanel();
        JButton switchStateButton=new JButton("切换视图");
        jPanel1.add(switchStateButton);

        Container c=getContentPane();    //获取当前窗口的内容窗格
        //c.add(jPanel1);
        c.add(jPanel0);
        //c.remove(jPanel0);
        //c.add(jPanel1);

//==========================================================
        //这段代码体现了View层不断查询Model层数据，用Model层的数据来更新视图
//==========================================================
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true)
                            jlabel0.setText(Model.getInstance().getText());
                    }
                }
        ).start();

        setVisible(true);    //设置窗口是否可见
    }
}